package com.mycompany.project.client.chee;

import java.util.ArrayList;
import java.util.Random;


import com.mycompany.project.client.ModuleType;

public class GPSData {
	/**
	 * The gps list.
	 */
	private ArrayList<ArrayList<Module>> gpsList;
	/**
	 * The key list.
	 */
	private ArrayList<Integer> keyList = new ArrayList<Integer>();
	/**
	 * THe random generator.
	 */
	private Random r = new Random();
	/**
	 * The constructor.
	 */
	public GPSData() {
		//initialized the gps list
		gpsList = new ArrayList<ArrayList<Module>>();
		for (int i = 0; i < Module.MAX_SIZE; i++) {
			gpsList.add(new ArrayList<Module>());
		}
	}
	/**
	 * This method will automatically create modules
	 * with random but unique data.
	 */
	public final void generateModules() {
		for (int i = 0; i < Module.MAX_SIZE; i++) {
			//clears whatever is in the list
			Random r = new Random();
			int m_size = r.nextInt(8 - 1) + 1;
			if (i == 0) m_size = r.nextInt(20 - 3) + 3;//plains at least up to 20
			else if (i == 4) m_size = r.nextInt(11 - 1) + 1;//dormitory at least up to 10
			for (int j = 0; j < m_size; j++) {
				ModuleType mType = Module.checkModuleType(i);
				Module m = new Module();
				m.setModuleType(mType);
				m.setUrl(
						Module.getModuleImage(
								mType.ordinal(
										)));
				m.setCodeNumber(getNewUniqueKey());
				int []coord = getUniqueXY();
				m.setPositionX(coord[1]);
				m.setPositionY(coord[0]);
				m.setStats(getRandomStatus());
				m.setOrientation(getRandomOrientation());
				gpsList.get(i).add(m);
			}
		}
	}
	/**
	 * This method will load the GPS data to the module list, map, and table.
	 */
	public void loadGPSData() {
		//clear the tables
		for (int i = 0; i <= ModuleList.getMlist().size(); i++) {
			LoggingModuleGUI.modulesFlexTableAll[i].removeAllRows();
			LoggingModuleGUI.modulesFlexTableAll[i].setHTML(
					0, 0, "<center><font size=\"1\">"
			+ "Code Number</font></center>");
			LoggingModuleGUI.modulesFlexTableAll[i].setHTML(
					0, 1, "<center><font size=\"1\">"
			+ "Position</font></center>");
			LoggingModuleGUI.modulesFlexTableAll[i].setHTML(
					0, 2, "<center><font size=\"1\">"
			+ "Status</font></center>");
			LoggingModuleGUI.modulesFlexTableAll[i].setHTML(
					0, 3, "<center><font size=\"1\">"
			+ "Orientation</font></center>");
			LoggingModuleGUI.modulesFlexTableAll[i].setHTML(
					0, 4, "<font size=\"1\">Delete</font>");
		}
		//Reset histogram
		LoggingModuleGUI.initHistogram();
		for (int i = 0; i < gpsList.size(); i++) {
			ModuleList.getMlist().get(i).clear();
			for (int j = 0; j < gpsList.get(i).size(); j++) {
				ModuleList.getMlist().get(i).add(
						gpsList.get(i).get(j));
				LoggingModuleGUI.TableInit(
						gpsList.get(i).get(j));
			}
		}
		//update label
		LoggingModuleGUI.totalM.setHTML(
				"Total Module(s):" + (
						ModuleList.getTotalModules())
				+ "&nbsp&nbsp&nbsp&nbsp usable:"
			    + ModuleList.getTotalUsableCount()
				+ "&nbsp&nbsp&nbsp&nbsp usable After Repair:"
				+ ModuleList.getTotalUsableAfterRepairCount()
				+ "&nbsp&nbsp&nbsp&nbsp Beyond Repair:"
				+ ModuleList.getTotalBeyondRepairCount());
		//init map
		MapGUI.initMap();
		//initialize confgui map
		ConfigurationGUI.redraw(0);
		ConfigurationGUI.redraw(1);
		ConfigurationGUI.setPlaceBtn(false);
		ConfigurationGUI.clearConfigMapList();
		ConfigurationGUI.showAllChanges();
	}
	/**
	 * Return a random orientation.
	 * @return the random integer value for orientation (0,1,2).
	 */
	public final int getRandomOrientation() {
		return r.nextInt(3);
	}
	/**
	 * Returns a string containing a status.
	 * @return the string for status.
	 */
	public final String getRandomStatus() {
		int choice = r.nextInt(Module.MAX_SIZE);
		if (choice >= 0 && choice <= 5) {
			return "usable";
		} else if (choice >= 6 && choice <= 8) {
			return "usable after repair";
		} else {
			return "beyond repair";
		}
	}
	/**
	 * This method will generate a unique x and y position.
	 * @return the integer array consisting of x and y position
	 * where index 0 is row and 1 is column.
	 */
	public static int[] getUniqueXY() {
		Random r = new Random();
		int []rowNCol = new int[2];
		boolean done = false;
		while (!done) {
			int countCheck = 0;
			rowNCol[0] = r.nextInt((MapGUI.ROW - 1) - 1) + 1;
			rowNCol[1] = r.nextInt((MapGUI.COL - 1) - 1) + 1;
			for (int i = 0; i < ModuleList.getMlist().size(); i++) {
				for (int j = 0; j < ModuleList.
						getMlist().get(i).size(); j++) {
					if (ModuleList.getMlist().
							get(i).get(j).
							getPositionX()
							== rowNCol[1]
							&& ModuleList.
							getMlist().get(i)
							.get(j).getPositionX()
							== rowNCol[0]) {
						if (!Rover.isOnRover(
								rowNCol[0],
								rowNCol[1])) {
							countCheck++;
						}
					}
				}
			}
			if (countCheck >= 1) {
				done = false;
			} else {
				done = true;
			}
		}
		return rowNCol;
	}
	/**
	 * This method generates a unique code number for a module.
	 * @return the new code number.
	 */
	public final int getNewUniqueKey() {
		int key = 0;
		boolean done = false;
		while (!done) {
			int countCheck = 0;
			key = r.nextInt(9999);
			if (keyList.isEmpty()){
				keyList.add(key);
				return key;
			} else {
				for (int i = 0; i < keyList.size(); i++) {
					if (key == keyList.get(i)) {
						countCheck++;
					}
				}
				if (countCheck >= 1) {
					done = false;
				} else {
					keyList.add(key);
					done = true;
				}
			}
		}
		return key;
	}
	/**
	 * Getter for keyList.
	 * @return keyList.
	 */
	public final ArrayList<Integer> getKeyList() {
		return keyList;
	}
	/**
	 * Getter for gpsList.
	 * @return gpsList.
	 */
	public final ArrayList<ArrayList<Module>> getGpsList() {
		return gpsList;
	}
}
